﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using TaxBusiness.Model;
using TaxBusiness.BLL;

namespace WebSites.Controllers
{
    class MenuTreeGrid
    {
        public int id { get; set; }
        public string Name { get; set; }
        public string WebIcon { get; set; }
        public string Url { get; set; }
        public int _parentId { get; set; }
    }

    public class SystemController : Controller
    {
        //
        // GET: /System/

        public ActionResult Customer()
        {
            return View();
        }

        public ActionResult AddCustomer()
        {
            return View();
        }

        public ActionResult UpdateCustomer()
        {
            return View();
        }

        public ActionResult Users()
        {
            return View();
        }

        public ActionResult AddUser()
        {
            return View();
        }

        public ActionResult ResetPWD()
        {
            return View();
        }

        public ActionResult GetMenuTree()
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            MenuBLL menubll = new MenuBLL();
            List<MenuData> menulist = new List<MenuData>();
            menulist = menubll.GetAll();
            List<MenuTreeGrid> menutree = new List<MenuTreeGrid>();
            foreach (MenuData menu in menulist)
            {
                MenuTreeGrid m = new MenuTreeGrid();
                m.id = menu.ID;
                m.Name = menu.Name;
                m.WebIcon = menu.WebIcon;
                m.Url = menu.Url;
                m._parentId = menu.ParentID;
                menutree.Add(m);
            }
            var menugrid = new
            {
                total = menutree.Count,
                rows = menutree
            };
            string json = serializer.Serialize(menugrid);
            return Content(json);
        }

        public ActionResult GetCustomerList()
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string order = Request.Form["order"].ToString();
            int pageIndex = int.Parse(Request.Form["page"].ToString());
            int pageSize = int.Parse(Request.Form["rows"].ToString());
            string keyWord = Request.Form["sort"].ToString();
            string whereString = "1=1";
            int count;
            if (Request.Form["customer"] != null && Request.Form["customer"].ToString() != "")
            {
                whereString += "and TheName like '%" + Request.Form["customer"].ToString() + "%'";
            }
            CustomerBLL customerbll = new CustomerBLL();
            List<Customer> customerlist = new List<Customer>();
            CustomerData listdata = new CustomerData();

            customerlist = customerbll.GetLlist(pageSize, pageIndex, whereString, keyWord, order, out count);
            listdata.total = count;
            listdata.rows = customerlist;
            string json = serializer.Serialize(listdata);
            return Content(json);
        }

        public ActionResult GetCustomers()
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string json = "";
            if (Request.Form["q"] != null && Request.Form["q"]!="")
            {
                CustomerBLL customerbll = new CustomerBLL();
                List<Customer> customerlist = new List<Customer>();
                string searchword = Request.Form["q"].ToString().Trim();
                customerlist = customerbll.GetLlist(searchword);
                json = serializer.Serialize(customerlist);
            }
            return Content(json);
        }

        public ActionResult SaveCustomer()
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            Message msg = new Message(false, "未保存成功。");
            Customer customer = new Customer();
            CustomerBLL customerbll = new CustomerBLL();
            int i = 0;
            try
            {
                customer.TheName = Request.Form["TheName"].ToString();
                customer.LegalPerson = Request.Form["LegalPerson"].ToString();
                customer.LPPhone = Request.Form["LPPhone"].ToString();
                string registeredCapital = Request.Form["RegisteredCapital"].ToString();
                string profit = Request.Form["Profit"].ToString();
                string annualIncome = Request.Form["AnnualIncome"].ToString();
                string foundDate = Request.Form["FoundDate"].ToString();
                customer.TaxNumber = Request.Form["TaxNumber"].ToString();
                customer.Phone = Request.Form["Phone"].ToString();
                customer.Contact = Request.Form["Contact"].ToString();
                customer.Addr = Request.Form["Addr"].ToString();

                if (registeredCapital == "")
                    customer.RegisteredCapital = null;
                else
                    customer.RegisteredCapital = Convert.ToDecimal(registeredCapital);
                if (profit == "")
                    customer.Profit = null;
                else
                    customer.Profit = Convert.ToDecimal(profit);
                if (annualIncome == "")
                    customer.AnnualIncome = null;
                else
                    customer.AnnualIncome = Convert.ToDecimal(annualIncome);
                if (foundDate == "")
                    customer.FoundDate = null;
                else
                    customer.FoundDate = Convert.ToDateTime(foundDate);
                i = customerbll.Save(customer);
            }
            catch (Exception e)
            {
                msg = new Message(false, e.Message);
            }
            if (i > 0)
            {
                msg = new Message(true, "保存成功。");
            }
            string json = serializer.Serialize(msg);
            return Content(json);
        }

        public ActionResult EditCustomer()
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            Message msg = new Message(false, "未保存成功。");
            Customer customer = new Customer();
            CustomerBLL customerbll = new CustomerBLL();
            int i = 0;
            try
            {
                customer.ID = int.Parse(Request.Form["ID"].ToString());
                customer.TheName = Request.Form["TheName"].ToString();
                customer.LegalPerson = Request.Form["LegalPerson"].ToString();
                customer.LPPhone = Request.Form["LPPhone"].ToString();
                string registeredCapital = Request.Form["RegisteredCapital"].ToString();
                string profit = Request.Form["Profit"].ToString();
                string annualIncome = Request.Form["AnnualIncome"].ToString();
                string foundDate = Request.Form["FoundDate"].ToString();
                customer.Phone = Request.Form["Phone"].ToString();
                customer.Contact = Request.Form["Contact"].ToString();
                customer.Addr = Request.Form["Addr"].ToString();

                if (registeredCapital == "")
                    customer.RegisteredCapital = null;
                else
                    customer.RegisteredCapital = Convert.ToDecimal(registeredCapital);
                if (profit == "")
                    customer.Profit = null;
                else
                    customer.Profit = Convert.ToDecimal(profit);
                if (annualIncome == "")
                    customer.AnnualIncome = null;
                else
                    customer.AnnualIncome = Convert.ToDecimal(annualIncome);
                if (foundDate == "")
                    customer.FoundDate = null;
                else
                    customer.FoundDate = Convert.ToDateTime(foundDate);
                i = customerbll.Update(customer);
            }
            catch (Exception e)
            {
                msg = new Message(false, e.Message);
            }
            if (i > 0)
            {
                msg = new Message(true, "修改成功。");
            }
            string json = serializer.Serialize(msg);
            return Content(json);
        }

        public ActionResult DeleteCustomer(string ids)
        {
            Message msg = new Message(true, "操作成功。");
            int i = 0;
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            CustomerBLL customerbll = new CustomerBLL();
            if (ids != null)
            {
                try
                {
                    i = customerbll.Delete(ids);
                }
                catch (Exception e)
                {
                    msg = new Message(false, e.Message);
                }
            }
            else
            {
                msg = new Message(false, "系统错误，请联系系统管理员：传值未成功。");
            }
            string json = serializer.Serialize(msg);
            return Content(json);
        }

        public ActionResult GetUsersList()
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            //string order = Request.Form["order"].ToString();
            //int pageIndex = int.Parse(Request.Form["page"].ToString());
            //int pageSize = int.Parse(Request.Form["rows"].ToString());
            //string keyWord = Request.Form["sort"].ToString();
            //string whereString = "1=1";
            UserBLL userbll = new UserBLL();
            List<Users> userslist = new List<Users>();
            UsersDate listdata = new UsersDate();

            userslist = userbll.GetAll();
            listdata.total = userslist.Count;
            listdata.rows = userslist;
            string json = serializer.Serialize(listdata);
            return Content(json);
        }

        public ActionResult SaveUsers()
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            UserBLL userbll = new UserBLL();
            Message msg = new Message(true, "操作成功。");
            Users users = new TaxBusiness.Model.Users();
            users.UserName = Request.Form["UserName"].ToString();
            users.TrueName = Request.Form["TrueName"].ToString();
            users.OfficeID = int.Parse(Request.Form["OfficeID"].ToString());
            users.AuthID = int.Parse(Request.Form["AuthID"].ToString());
            users.PWD = "123456";
            string menuitem = Request.Form["ids"];
            users.MenuID = menuitem;
            try
            {
                userbll.Save(users, menuitem);
            }
            catch (Exception e)
            {
                msg = new Message(false, e.Message);
            }
            string json = serializer.Serialize(msg);
            return Content(json);
        }

        public ActionResult EditUsers()
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            UserBLL userbll = new UserBLL();
            Message msg = new Message(true, "操作成功。");
            Users users = new TaxBusiness.Model.Users();
            users.ID = int.Parse(Request.Form["ID"].ToString());
            users.UserName = Request.Form["UserName"].ToString();
            users.TrueName = Request.Form["TrueName"].ToString();
            users.OfficeID = int.Parse(Request.Form["OfficeID"].ToString());
            users.AuthID = int.Parse(Request.Form["AuthID"].ToString());
            string menuitem = Request.Form["ids"];
            users.MenuID = menuitem;
            try
            {
                userbll.update(users, menuitem);
            }
            catch (Exception e)
            {
                msg = new Message(false, e.Message);
            }
            string json = serializer.Serialize(msg);
            return Content(json);
        }

        public ActionResult DeleteUsers(string ids)
        {
            Message msg = new Message(true, "操作成功。");
            int i = 0;
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            UserBLL userbll = new UserBLL();
            if (ids != null)
            {
                try
                {
                    i = userbll.Delete(ids);
                }
                catch (Exception e)
                {
                    msg = new Message(false, e.Message);
                }
            }
            else
            {
                msg = new Message(false, "系统错误，请联系系统管理员：传值未成功。");
            }
            string json = serializer.Serialize(msg);
            return Content(json);
        }

        public ActionResult SetUserDown(string ids)
        {
            Message msg = new Message(true, "操作成功。");
            int i = 0;
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            UserBLL userbll = new UserBLL();
            if (ids != null)
            {
                try
                {
                    i = userbll.SetActivate(ids, false);
                }
                catch (Exception e)
                {
                    msg = new Message(false, e.Message);
                }
            }
            else
            {
                msg = new Message(false, "系统错误，请联系系统管理员：传值未成功。");
            }
            string json = serializer.Serialize(msg);
            return Content(json);
        }

        public ActionResult SavePWD()
        {
            Message msg = new Message(false, "重设密码错误。");
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string pwd = "";
            int userid = 0;
            int i = 0;
            if (Request.Form["newpassword"] != null && Request.Form["newpassword"].ToString() != "")
            {
                pwd = Request.Form["newpassword"].ToString();
                userid = int.Parse(Session["UserID"].ToString());
                UserBLL userbll = new UserBLL();
                try
                {
                    i = userbll.SetPWD(userid, pwd);
                }
                catch (Exception e)
                {
                    msg = new Message(false, e.Message);
                }
            }
            if (i == 1)
                msg = new Message(true, "重设密码完成。");
            string json = serializer.Serialize(msg);
            return Content(json);
        }

    }
}
